<?php

require_rewrite(MODULE_ROOT . "manager/admin/LogManager.class.php");

class CreateadminController extends BaseController {

    private $method;

    public function __construct() {
        parent::__construct();
        $this->method = Handler_http::getMethod();
    }

    /**
     * 管理员列表
     */
    public function adminList() {
        $AdminList = new CreateAdminModule();
        $list = $AdminList->AdminList($this->directoryId);
        if ($list) {
            $this->tpl->assign('Adminlist', $list);
        }
        $this->tpl->assign('userID', $this->username_id);
        $this->tpl->assign('createTime', $this->createTime);
        $this->tpl->display("create_admin/admin_list.html");
    }

    /**
     * 添加管理员账号
     */
    public function addAdminUser() {
        $createAdmin = new CreateAdminModule();
        $pwd_arr = Handler_validate::getExpressions();
        if ($this->method == "POST") {
            $user['username'] = $_POST['txtusername'];
            $user['txtpassword'] = $_POST['txtpassword'];
            $user['txtpassword_confirm'] = $_POST['txtpassword_confirm'];
            $user['directoryId'] = $this->directoryId;
            if (empty($user['txtpassword']) || empty($user['txtpassword_confirm']) || empty($user['username'])) {
                Handler_tool::alertAndRedirect('请输入管理员信息！', HTTPFILEURL . '/createadmin/addAdminUser/');
            }
            if ($user['txtpassword'] != $user['txtpassword_confirm']) {
                Handler_tool::alertAndRedirect('两次输入的密码不同，请重新输入！', HTTPFILEURL . '/createadmin/addAdminUser/');
            }
            if (!preg_match($pwd_arr['str'], $user['txtpassword']) || strlen($user['txtpassword']) != intval($pwd_arr['length'])) {
                Handler_tool::alertAndRedirect('请输入指定格式组成的密码！', HTTPFILEURL . '/createadmin/addAdminUser/');
            }
            switch ($createAdmin->addAdminUser($user)) {
                case 1:
                    Handler_tool::alertAndRedirect('该用户名已存在,请重新输入新用户名称！', HTTPFILEURL . '/createadmin/addAdminUser/');
                    break;
                case 2:
                    $this->auth_add_sql_log(1, $user['username']);
                    Handler_tool::alertAndRedirect('管理员账号添加成功！', HTTPFILEURL . '/createadmin/adminList/');
                    break;
                default :
                    $this->auth_add_sql_log(4, $user['username']);
                    Handler_tool::alertAndRedirect('添加失败！', HTTPFILEURL . '/createadmin/addAdminUser/');
                    break;
            }
        }
        $this->tpl->assign('pwdTxt', $pwd_arr);
        $this->tpl->display("create_admin/add_admin_user.html");
    }

    /**
     * 删除管理员用户
     */
    public function delAdminUser() {
        $userid = $_GET['userid'];
        if (!empty($userid)) {
            $data = UserDao::getUserList($userid);
            $username = $data[0]['username'];
            $row = new CreateAdminModule();
            if ($row->delAdminUser($userid)) {
                $this->auth_add_sql_log(3, $username);
                Handler_tool::alertAndRedirect('管理员账号删除操作成功!', HTTPFILEURL . '/createadmin/adminList/');
            }else{
                $this->auth_add_sql_log(6, $username);
                Handler_tool::alertAndRedirect('管理员账号删除操作失败!', HTTPFILEURL . '/createadmin/adminList/');
            }
        }
    }

    /**
     * 记录用户操作审核日志
     */
    private function auth_add_sql_log($auth, $username) {
        switch ($auth) {
            case 1:
                $msg = '添加管理员成功';
                break;
            case 2:
                $msg = '编辑管理员成功';
                break;
            case 3:
                $msg = '删除管理员成功';
                break;
            case 4:
                $msg='添加管理员失败';
                break;
            case 5:
                $msg='编辑管理员失败';
                break;
            case 6:
                $msg='删除管理员失败';
                break;
        }
        $log_id = LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], $msg . $username);
        $auditlog = LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
        UserDao::sqlUpUserBakLog($username, $auditlog);
    }

}

?>